﻿/* ***** BEGIN LICENSE BLOCK *****
 *
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * 
 * The Original Code is CnCNet Client code, Released 2nd January 2009.
 * 
 * The Initial Developers of the Original Code are
 * Adam Krock and John Sanderson. Portions created by
 * the Initial Developer are Copyright (C) 2010
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s): Adam Krock, John Sanderson.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either of the GNU General Public License Version 2 or later (the "GPL"),
 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 * 
 * ***** END LICENSE BLOCK *****/

using System;
using System.Runtime.InteropServices;
using System.Text;

namespace CnCNet
{
    /// <summary>
    /// Added by John on 17/04/2009.
    /// Interfaces with conquer.ini
    /// </summary>
    public class cncNetIni
    {
        #region Variables and Externals

        /// <summary>
        /// Added by John on 17/04/2007
        /// A serialized string of settings used for online play.
        /// </summary>
        public const string onlineVars = "2=0=0=0=6=0=0=0=0=0=0=0=0";
        private string path;
        private string originalVars;

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section,
            string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section,
                 string key, string def, StringBuilder retVal,
            int size, string filePath);

        #endregion

        /// <summary>
        /// Added by John on 17/04/2009.
        /// Initializes a new instance of the <see cref="cncNetIni"/> class.
        /// </summary>
        /// <param name="cc95path">The path of C&C95.exe.</param>
        public cncNetIni(string cc95path)
        {
            path = cc95path.Replace("C&C95.exe", "conquer.ini").Replace("\"", "");
        }

        #region Read and Write Methods

        /// <summary>
        /// Added by John on 17/04/2009
        /// Writes a value to conquer.ini
        /// </summary>
        /// <param name="Section">The section containing the key.</param>
        /// <param name="Key">The key to write to.</param>
        /// <param name="Value">The value to write to the key.</param>
        public void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, this.path);
        }


        /// <summary>
        /// Added by John on 17/04/2009
        /// Reads a value from conquer.ini
        /// </summary>
        /// <param name="Section">The section containing the key.</param>
        /// <param name="Key">The key to read from.</param>
        /// <returns></returns>
        public string IniReadValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
            return temp.ToString();
        }

        #endregion

        #region Conquer.ini Specific Methods

        /// <summary>
        /// Added by John on 17/04/2009
        /// Sets the variables in conquer.ini for online play.
        /// </summary>
        public void setVarsForOnline()
        {
            originalVars = IniReadValue("Options", "Resolution");
            originalVars += "=" + IniReadValue("Options", "HardwareFills");
            originalVars += "=" + IniReadValue("Options", "VideoBackBuffer");
            originalVars += "=" + IniReadValue("Options", "Compatibility");

            originalVars += "=" + IniReadValue("Advanced", "LANPlayers");
            originalVars += "=" + IniReadValue("Advanced", "ParanoidAI");
            originalVars += "=" + IniReadValue("Advanced", "WheeledRotation");
            originalVars += "=" + IniReadValue("Advanced", "SeparateHelipad");
            originalVars += "=" + IniReadValue("Advanced", "MCVUndeploy");
            originalVars += "=" + IniReadValue("Advanced", "NoBibs");
            originalVars += "=" + IniReadValue("Advanced", "CombatIQ");
            originalVars += "=" + IniReadValue("Advanced", "TiberiumOverrun");
            originalVars += "=" + IniReadValue("Advanced", "Funpark");

            setOriginalVars(onlineVars);
        }


        /// <summary>
        /// Added by John on 17/04/2009
        /// Resets the variables in conquer.ini based on the internal class backup.
        /// </summary>
        public void setOriginalVars()
        {
            setOriginalVars(this.originalVars);
        }


        /// <summary>
        /// Added by John on 17/04/2009
        /// Sets the variables in conquer.ini.
        /// </summary>
        /// <param name="originalVariables">The serialized string of variables to set to conquer.ini</param>
        public void setOriginalVars(string originalVariables)
        {
            string[] vars = originalVariables.Split(new string[] { "=" }, StringSplitOptions.None);

            //IniWriteValue("Options", "Resolution", vars[0]);
            IniWriteValue("Options", "HardwareFills", vars[1]);
            IniWriteValue("Options", "VideoBackBuffer", vars[2]);
            IniWriteValue("Options", "Compatibility", vars[3]);

            IniWriteValue("Advanced", "LANPlayers", vars[4]);
            IniWriteValue("Advanced", "ParanoidAI", vars[5]);
            IniWriteValue("Advanced", "WheeledRotation", vars[6]);
            IniWriteValue("Advanced", "SeparateHelipad", vars[7]);
            IniWriteValue("Advanced", "MCVUndeploy", vars[8]);
            IniWriteValue("Advanced", "NoBibs", vars[9]);
            IniWriteValue("Advanced", "CombatIQ", vars[10]);
            IniWriteValue("Advanced", "TiberiumOverrun", vars[11]);
            IniWriteValue("Advanced", "Funpark", vars[12]);
        }

        /// <summary>
        /// Added by John on 17/04/2009
        /// Gets the internal class backup of the original variables.
        /// </summary>
        /// <returns>The internal class backup of the original varaibles.</returns>
        public string getOriginalVars()
        {
            return this.originalVars;
        }

        #endregion

        /// <summary>
        /// Added by John on 17/04/2009
        /// Gets the path of of the ini file.
        /// </summary>
        /// <returns>The path of the ini file.</returns>
        public string getPath()
        {
            return this.path;
        }
    }
}